%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
clear all; close all; clc;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Modify main_str to match path on your machine
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
main_str = 'C:\Users\Marco\Dropbox\Housing_Brainstorming\Information_disclosure';
% main_str = 'L:\agargano\Dropbox\Housing_Brainstorming\Information_disclosure';

save_str = [main_str,'\Replication_code_RFS\Model'];

% grid of values of sigma_{\psi}
endowM_grid       = (0.01:0.01:0.100)';
n_endow           = size(endowM_grid,1);

for bb = 1:n_endow

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %% Seller Reservation and Distribution of Private Values

    seller_reservation     = 650000;
    bidders_mean           = 650000;
    % bidders_std            = 65000;
    bidders_std            = 32500;
    bidders_logstd         = sqrt(log((bidders_std^2)/exp(2*log(bidders_mean))+1));
    bidders_logmean        = log(bidders_mean)-((bidders_logstd^2)/2);

    n_sim                  = 1000000; % number of random draws for bids
    
    start_bid_scale        = (0.75:0.010:1.25);
    start_bid              = start_bid_scale*seller_reservation;  % vector of entry bids (1+alpha)*seller reservation
    n_start                = size(start_bid,2);

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % select value of sigma_{\psi}
    mu_endow                = endowM_grid(bb);    

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    n_bidders_all_vec      = (1:1:15); % number of bidders
    i_all                  = size(n_bidders_all_vec,2);

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %% Simulations

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % Allocate Memory
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    Psales_mat       = nan(i_all,n_start);
    Ebidders_mat     = nan(i_all,n_start);
    EbidderW_mat     = nan(i_all,n_start);
    EbidderW_obj_mat = nan(i_all,n_start);
    EoverbidL_mat    = nan(i_all,n_start);    
    EwinW_mat        = nan(i_all,n_start);    
    Esales_mat       = nan(i_all,n_start);
    Erev_mat         = nan(i_all,n_start);
 
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    poolobj         = parpool('local');
    % % tic;

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    parfor i = 1:i_all % paraller loop over number of bidders

        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        start_bid_curr   = start_bid; % entry bid vector
        bid_values_mat   = lognrnd(bidders_logmean,bidders_logstd,n_bidders_all_vec(i),n_sim); % draw pre-auction private values
        if i > 1
             bid_values_mat = sort(bid_values_mat,'descend'); % sort private values
        end

        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%    
        for k = 1:n_start % loop over entry bids

            %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%    
            % Allocate Memory
            %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%    
            sales_succ_vec       = zeros(n_sim,1);
            bidder_value_vec     = zeros(n_sim,1);
            bidder_value_obj_vec = zeros(n_sim,1);            
            sales_price_vec      = nan(n_sim,1);
            nbidders_vec         = zeros(n_sim,1);            
        
            %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%            
            for n = 1:n_sim % loop over draws of private values 
    
                %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
                bids_values_vec       = bid_values_mat(:,n); % private values            
                is_act                = bids_values_vec>start_bid_curr(k); % which bidders have private value above entry bid? 

                %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%                
                nbidders_vec(n,1)     = sum(is_act); % num bidders with private value above entry bid (enter the auction)
                
                %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%     
                if nbidders_vec(n,1) == 1 % if only one bidder enters
                     sales_price_vec(n,1)      = start_bid_curr(k);
                     bidder_value_vec(n,1)     = bids_values_vec(1)-start_bid_curr(k);  
                     bidder_value_obj_vec(n,1) = bids_values_vec(1)-start_bid_curr(k);   
                     sales_succ_vec(n,1)       = 1; % house is sold   
                elseif nbidders_vec(n,1) > 1  % if multiple bidders enter   
                     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
                     endow_vec                 = exprnd(mu_endow,nbidders_vec(n,1),1); % draw of exponential valuation shocks
                     bids_values_endow_vec     = bids_values_vec(1:nbidders_vec(n,1)).*(1+endow_vec); % bidders who entered have valuation impacted by valuation shocks  
                     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%                     
                     [bids_values_endow_vec,sel_orig] = sort(bids_values_endow_vec,'descend'); % sort values after valuation shocks
                     %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
                     sales_price_vec(n,1)      = bids_values_endow_vec(2); % sales price (second highest value after shock)
                     bidder_value_vec(n,1)     = bids_values_endow_vec(1)     - sales_price_vec(n,1); % post-shock surplus 
                     bidder_value_obj_vec(n,1) = bids_values_vec(sel_orig(1)) - sales_price_vec(n,1); % pre-shock surplus   
                     sales_succ_vec(n,1)       = 1;  % house is sold    
                end    
            
            end
     
            %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
            Psales_mat(i,k)        = sum(sales_succ_vec)/n_sim; % sales probability
            Ebidders_mat(i,k)      = nanmean(nbidders_vec)/n_bidders_all_vec(i); % expected share of bidders who enter the auction
            EbidderW_mat(i,k)      = nanmean(bidder_value_vec)/n_bidders_all_vec(i); % expected surplus
            EbidderW_obj_mat(i,k)  = nanmean(bidder_value_obj_vec)/n_bidders_all_vec(i); % expected surplus (based on pre-shock values)
            EoverbidL_mat(i,k)     = nanmean((bidder_value_obj_vec(sales_succ_vec==1,1)<0)); % probability that the winning bidder realizes negative surplus
            EwinW_mat(i,k)         = nanmean((bidder_value_obj_vec(sales_succ_vec==1,1))); % difference between sales price and pre-shock private value of winning bidder
            Esales_mat(i,k)        = nanmean(sales_price_vec)-seller_reservation; % expected sales price - minus seller reservation value
            Erev_mat(i,k)          = Psales_mat(i,k)*Esales_mat(i,k); % expected seller revenues       
            %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%        
        
        end
    
    end

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % toc;
    delete(poolobj);

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %% Save

    save([save_str,'\Output\Last\EndowInd\equilibrium_step1_last_endow_mu',num2str(endowM_grid(bb)*100),'.mat']);
    
    endowM_grid(bb)

    clearvars -except endowM_grid n_endow bb main_str save_str

end

